Skip to main content

CNC Example 07: Using Expressions and Subprograms

See the CNC07_Subprogram.project sample project in the installation directory of CODESYS under ..\CODESYS SoftMotion\Examples.

This example demonstrates how to call a subprogram from a CNC file as well as use expressions in CNC motion instructions.

In the CNC program, the drives are first switched on, as in the other examples. The CNC_PreparePath program uses the SMC_ReadNCFile2 function block for reading the CNC file. SMC_ReadNCFile2 creates the SMC_ReadNCFile POU and extends it with support for subprograms and expressions.

The CNC program CNC2Main.cnc is converted by means of the SMC_NCInterpreter function block. SMC_NCInterpreter creates the SMC_NCDecoder POU and extends it with support for subroutines and expressions.

The project contains two CNC programs: the main program CNC2Main.cnc and the subprogram CNC2.cnc. The main program passes the value 25 in the #RADIUS parameter to the subprogram. In the subprogram, the expression X#RADIUS*2 is used for calculating the circular path.

Main program

N0 G01 X$g_x$ Y0 F50 E30 E-30
N10 G01 X0 Y$g_y$
N20 CNC2{25}
N30 G01 X0 Y-200

Subprogram

SUBPROGRAM CNC2{#RADIUS : LREAL}
N010 G91
N020 G02 X#RADIUS*2 Y0 R#RADIUS Z2.5 F100 E-100 E100
N030 G02 X-#RADIUS*2 Y0 R#RADIUS Z7.5
END_SUBPROGRAM